import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export const menuList: any = [
  {
    key: 'sealApply',
    name: '我的申请',
    icon: require('@/assets/seal-apply.png'),
    children: [
      {
        path: '/home/newApply',
        name: 'newApply',
        meta: {
          title: '新领申请',
          icon: '',
          holds: ['applyPage'],
        },
        component: () => import('@/views/NewApply'),
      },
      {
        path: '/home/revokeApply',
        name: 'revokeApply',
        meta: {
          title: '吊销申请',
          icon: '',
        },
        component: () => import('@/views/RevokeApply'),
      },
      {
        path: '/home/reissueApply',
        name: 'reissueApply',
        meta: {
          title: '补发申请',
          icon: '',
        },
        component: () => import('@/views/ReissueApply'),
      },
      {
        path: '/home/lossApply',
        name: 'lossApply',
        meta: {
          title: '挂失申请',
          icon: '',
        },
        component: () => import('@/views/LossApply'),
      },
      {
        path: '/home/recoverApply',
        name: 'recoverApply',
        meta: {
          title: '恢复申请',
          icon: '',
        },
        component: () => import('@/views/RecoverApply'),
      },
      {
        path: '/home/organizationManagement',
        name: 'organizationManagement',
        meta: {
          title: '组织机构更新申请',
          icon: '',
          holds: ['organizationApply', 'organizeUpdate'],
        },
        component: () => import('@/views/organizationManagement'),
      },
      {
        path: '/home/agencyAdministratorApply',
        name: 'agencyAdministratorApply',
        meta: {
          title: '机构印章管理员申请',
          icon: '',
          holds: ['administratorApply'],
        },
        component: () => import('@/views/AgencyAdministratorApply'),
      },
    ],
  },
  {
    key: 'appliedForMe',
    name: '我申请的印章',
    icon: require('@/assets/applied-for-me.png'),
    children: [
      {
        path: '/home/appliedForMe',
        name: 'appliedForMe',
        meta: {
          title: '申请印章列表',
          icon: '',
        },
        component: () => import('@/views/AppliedForMe'),
      },
    ],
  },
  {
    key: 'myManagingSeal',
    name: '我管理的印章',
    icon: require('@/assets/myManagingSeal.png'),
    children: [
      {
        path: '/home/myManagingSeal',
        name: 'myManagingSeal',
        meta: {
          title: '管理印章列表',
          icon: '',
          holds: ['manageSealRefProject', 'authProject'],
        },
        component: () => import('@/views/MyManagingSeal'),
      },
      {
        path: '/home/authorizeApp/:type',
        name: 'authorizeApp',
        meta: {
          title: '授权应用列表',
          icon: '',
          defaultPath: '/home/authorizeApp/cloud',
          holds: [
            'authorizeNew',
            'authorizeOthers',
            'authorizeUpdate',
            'authorizeAppSeal',
          ],
        },
        component: () => import('@/views/AuthorizeApp'),
      },
    ],
  },
  // 临时添加，帮助收货地址地址开发
  {
    key: 'user',
    name: '个人中心',
    icon: require('@/assets/user.png'),
    children: [
      {
        path: '/home/userInfo',
        name: 'userInfo',
        meta: {
          title: '个人信息',
          icon: '',
          holds: ['mobileUpdate'],
        },
        component: () => import('@/views/UserInfo'),
      },
      {
        path: '/home/myOrderList/:type',
        name: 'myOrderList',
        meta: {
          title: '我的订单',
          icon: '',
          defaultPath: '/home/myOrderList/all',
        },
        component: () => import('@/views/MyOrderList'),
      },
      {
        path: '/home/shippingAddress',
        name: 'shippingAddress',
        meta: {
          title: '收货地址',
          icon: '',
        },
        component: () => import('@/views/shippingAddress'),
      },
    ],
  },
]

export function getMenus() {
  return menuList.reduce((menu1: any, menu2: any) => [
    ...(menu1.children || menu1),
    ...menu2.children,
  ])
}

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/hand-write',
      name: 'handWrite',
      component: () => import('@/views/HandWriteView'),
    },
    {
      path: '/result',
      name: 'resultPage',
      component: () => import('@/views/HandWriteView/ResultPage'),
    },
    {
      path: '/homepage',
      name: 'homepage',
      redirect: '/homepage/first',
      component: () => import('@/views/HomePage'),
      children: [
        {
          path: '/homepage/first',
          name: 'first',
          component: () => import('@/views/HomePage/First'),
        },
        {
          path: '/homepage/news',
          name: 'news',
          component: () => import('@/views/HomePage/News'),
        },
        {
          path: '/homepage/download',
          name: 'download',
          component: () => import('@/views/HomePage/Download'),
        },
        {
          path: '/homepage/faq',
          name: 'faq',
          component: () => import('@/views/HomePage/FAQ'),
        },
        {
          path: '/homepage/news-detail',
          name: 'newsDetail',
          component: () => import('@/views/HomePage/NewsDetail'),
        },
      ],
    },
    {
      path: '/applyForm',
      name: 'applyForm',
      component: () => import('@/views/ApplyForm'),
    },
    {
      path: '/adminApply',
      name: 'adminApply',
      component: () => import('@/views/AdminApply'),
    },
    {
      path: '/updateForm',
      name: 'updateForm',
      component: () => import('@/views/UpdateForm'),
    },
    {
      path: '/home',
      name: 'home',
      redirect: '/home/sealApply',
      component: () => import('@/views/Home'),
      children: [
        ...getMenus(),
        {
          path: '/home/applyPage/:type?',
          name: 'applyPage',
          component: () => import('@/views/ApplyPage'),
        },
        {
          path: '/home/paymentOrder/:type?',
          name: 'paymentOrder',
          meta: {
            title: '',
            icon: '',
          },
          component: () => import('@/views/PaymentOrder'),
        },
        {
          path: '/home/orderConfirm/:type?',
          name: 'orderConfirm',
          meta: {
            title: '在线支付',
            icon: '',
          },
          component: () => import('@/views/OrderConfirm'),
        },
        {
          path: '/home/payResult/:type?',
          name: 'payResult',
          meta: {
            title: '支付结果',
            icon: '',
          },
          component: () => import('@/views/PayResult'),
        },
        {
          path: '/home/organization/:type?',
          name: 'organizationDetail',
          component: () => import('@/views/organizationManagement/detail'),
        },
        {
          path: '/home/organizationApply/:type?',
          name: 'organizationApply',
          component: () => import('@/views/organizationManagement/apply'),
          meta: {
            title: '创建组织机构',
            icon: '',
          },
        },
        {
          path: '/home/organizeUpdate/:type?',
          name: 'organizeUpdate',
          component: () => import('@/views/organizationManagement/update'),
          meta: {
            title: '更新组织机构',
            icon: '',
          },
        },
        {
          path: '/home/administratorApply/:type?',
          name: 'administratorApply',
          meta: {
            title: '机构印章管理员在线申请',
            icon: '',
          },
          component: () =>
            import('@/views/AgencyAdministratorApply/administratorApply'),
        },
        {
          path: '/home/applyFlow/:type?',
          name: 'applyFlow',
          component: () => import('@/views/ApplyFlow'),
        },
        {
          path: '/home/mobileUpdate/:type?',
          name: 'mobileUpdate',
          component: () => import('@/views/MobileUpdate'),
          meta: {
            title: '个人信息',
            icon: '',
          },
        },
        {
          path: '/home/authorizeNew',
          name: 'authorizeNew',
          component: () => import('@/views/AuthorizeNew'),
          meta: {
            title: '授权新应用',
            icon: '',
            back: 'authorizeApp',
          },
        },
        {
          path: '/home/authorizeUpdate/:type',
          name: 'authorizeUpdate',
          component: () => import('@/views/AuthorizeUpdate'),
          meta: {
            title: { cancel: '取消授权印章', auth: '授权印章' },
            icon: '',
            isBack: true,
          },
        },
        {
          path: '/home/authProject/:type',
          name: 'authProject',
          component: () => import('@/views/AuthProject'),
          meta: {
            title: { cancel: '取消授权应用', auth: '授权应用' },
            icon: '',
            isBack: true,
          },
        },
        {
          path: '/home/authorizeOthers',
          name: 'authorizeOthers',
          component: () => import('@/views/AuthorizeNew/AuthorizeOthers'),
          meta: {
            title: '同步到其他应用',
            icon: '',
            back: 'authorizeApp',
          },
        },
        {
          path: '/home/authorizeAppSeal',
          name: 'authorizeAppSeal',
          component: () => import('@/views/AuthorizeApp/AuthorizeAppSeal'),
          meta: {
            title: '查看授权印章',
            icon: '',
            isBack: true,
          },
        },
        {
          path: '/home/manageSealRefProject',
          name: 'manageSealRefProject',
          component: () => import('@/views/ManageSealRefProject'),
          meta: {
            title: '查看授权应用',
            icon: '',
            isBack: true,
          },
        },
        {
          path: '/home/projectDetail',
          name: 'projectDetail',
          component: () => import('@/views/ProjectDetail'),
          meta: {
            title: '应用详情',
            icon: '',
            isBack: true,
          },
        },
        {
          path: '/home/authorizeResult',
          name: 'authorizeResult',
          component: () => import('@/views/AuthorizeNew/Result'),
          meta: {
            title: '授权结果',
            icon: '',
          },
        },
        {
          path: '/home/*',
          redirect: '/home/newApply',
        },
      ],
    },
    {
      path: '/support',
      name: 'support',
      component: () => import('@/views/help/index.tsx'),
    },
    {
      path: '/signup',
      name: 'signup',
      component: () => import('@/views/signup/index.tsx'),
    },
    {
      path: '/*',
      redirect: '/homepage',
    },
  ],
})
